package inl.resilienceManager.qmul.gemom;

import resilienceManager.qmul.gemom.Gbroker;
import resilienceManager.qmul.gemom.NSitem;

/** COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 * */

public class solution {
//	solution of one broker
	// the id of gbroker
	private int id;
	
	// The list of item
	private NSitem[] nslist;
	
	// The allocated string
	private String allocated;
	
	
	// the allocated item
	private NSitem[] alist;
	
	/**
	 * 
	 * 
	 * **/
	
	public solution(int idg, String as, NSitem[] ns)
	{
		id=idg;
		
		nslist=ns;
//		in string allocated, item no. start with 1 
		allocated=as;
		
		if(allocated!=""){
			find();
		}else{
//			alist = new NSitem[0];
		}
	}
	
/*	for total item set i allocated to broker   
	Ri - Pi*Pr(Vi>C)
*/	
	double getRavenue(NSitem iset, int id){
		return 0;
	}
	
	double getPenalty(NSitem iset, int id){
		return 0;
	}
	
	double getUtility(solution s){
		return 0;
	}
	
	public int getGID(){
		return id;
	}
	
	public NSitem[] getAlist(){
		return alist;
	}
	
	private void find(){
		
		
		String splitPattern="-";
		
		String[] xs=allocated.split(splitPattern);
		
		alist= new NSitem[xs.length];
		
		for(int i=0; i<xs.length; i++)
		{
			
			int id=Integer.parseInt(xs[i]);
//		TODO fix the item no.	find the 
			alist[i]=nslist[id-1];
			
		}	
	}
	
	
	public String toString(){
		
		return allocated;
		
	}
	
// mean rate of this alist 
	public double Rate(){
		double sumRate=0;
		for(int i=0; i< alist.length; i++)
		{
			sumRate=sumRate+alist[i].Rate();
		}
		return sumRate;
	}
	
//	no. of namespaces in alist
	public double Size(){
		
		double sum=0;
		if (alist==null || alist.length==0){
			return 0;
		}
		
		for(int i=0; i< alist.length; i++)
		{
			sum=sum+alist[i].getSize();
		}
		
		return sum;
	}
	
	
	public double variance(){
		
		double v=0;
		
		v= VarianceCalculation.sumItem(alist);
		
		return v;
		
	}
	



}
